<template>  
  <div class="system-ys7Cameras-edit">
    <!-- 添加或修改萤石云设备对话框 -->
    <el-dialog v-model="isShowDialog" width="769px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-ys7Cameras-edit .el-dialog', '.system-ys7Cameras-edit .el-dialog__header']">{{(!formData.id || formData.id==0?'添加':'修改')+'萤石云设备'}}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="90px">        
        <el-form-item label="设备名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入设备名" />
        </el-form-item>       
        <el-form-item label="任务开关" prop="jiguang_schedule">
          <el-switch
    v-model="formData.jiguang_schedule"
    class="ml-2" :active-value="1"
      :inactive-value="0"
    style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
  />
        </el-form-item>     
        <el-form-item label="打开时间" prop="jiguang_open_at">
          <el-time-picker :disabled="formData.jiguang_schedule!=1" value-format="HH:mm:ss"  v-model="formData.jiguang_open_at" placeholder="请选择打开时间" />
        </el-form-item>     
        <el-form-item label="关闭时间" prop="jiguang_close_at">
          <el-time-picker :disabled="formData.jiguang_schedule!=1" value-format="HH:mm:ss" v-model="formData.jiguang_close_at" placeholder="请选择关闭时间" />
        </el-form-item>     
        <el-form-item label="模板" prop="schedule_id">
          <el-select
									v-model="formData.schedule_id"
								placeholder="请选择角色"
									style="width: 100%" 
								>
									<el-option v-for="item of dataList"  :key="item.id" :label="item.name" :value="item.id"> </el-option>
								</el-select>
        </el-form-item>     
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import {reactive, toRefs, defineComponent, ref, unref, getCurrentInstance, onMounted} from 'vue';
import { ElMessage} from 'element-plus';
import {
  getYs7Cameras,
  addYs7Cameras,
  updateYs7Cameras,  
} from "/@/api/system/ys7Cameras";
import {
  Ys7CamerasInfoData,
} from "/@/views/system/ys7Cameras/list/component/model"
import {
    listYs7CamerasSchedule,
} from "/@/api/system/ys7CamerasSchedule";
export default defineComponent({
  name:"apiV1SystemYs7CamerasEdit",
  components:{    
  },
  props:{    
    statusOptions:{
      type:Array,
      default:()=>[]
    },    
  },
  setup(props,{emit}) {    
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();    
    const state = reactive({
      loading:false,
      isShowDialog: false,
      formData: {        
        id: undefined,        
        name: undefined,        
        deviceSerial: undefined,        
        channelNo: undefined,        
        createdAt: undefined,        
        connectTime: undefined,        
        disconnectTime: undefined,        
        status: false ,        
        poster: undefined,        
        pcLive: undefined,        
        mobileLive: undefined,        
        pcRec: undefined,        
        mobileRec: undefined,    jiguang_close_at:'',jiguang_schedule:0,
        jiguang_open_at:'',schedule_id:''
      },
      // 表单校验
      rules: {        
        id : [
            { required: true, message: "序号不能为空", trigger: "blur" }
        ],        
        name : [
            { required: true, message: "设备名不能为空", trigger: "blur" }
        ],        
        // jiguang_close_at : [
        //     { required: true, message: "关闭时间不能为空", trigger: "blur" }
        // ],        
        // jiguang_open_at : [
        //     { required: true, message: "打开时间不能为空", trigger: "blur" }
        // ],        
      },
      dataList:[]
    });
    // 打开弹窗
    const openDialog = (row?: Ys7CamerasInfoData) => {
      resetForm();
    ys7CamerasScheduleList()
      if(row) {        
        getYs7Cameras(row.id!).then((res:any)=>{
          const data = res.data;          
          state.formData = data;
          state.formData.schedule_id = Number(state.formData.schedule_id);
      })
    }
      state.isShowDialog = true;
    };
    // 获取列表数据
    const ys7CamerasScheduleList = ()=>{
          listYs7CamerasSchedule({ pageNum: 1,pageSize: 1000,}).then((res:any)=>{
            let list = res.data.list??[];            
            state.dataList = list;            
          })
        };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          if(!state.formData.id || state.formData.id===0){
            //添加
          addYs7Cameras(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('ys7CamerasList')
            }).finally(()=>{
              state.loading = false;
            })
          }else{
            //修改
          updateYs7Cameras(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('ys7CamerasList')
            }).finally(()=>{
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        id: undefined,        
        name: undefined,        
        deviceSerial: undefined,        
        channelNo: undefined,        
        createdAt: undefined,        
        connectTime: undefined,        
        disconnectTime: undefined,        
        status: false ,        
        poster: undefined,        
        pcLive: undefined,        
        mobileLive: undefined,        
        pcRec: undefined,        
        mobileRec: undefined,      jiguang_close_at:'',jiguang_schedule:0,
        jiguang_open_at:''  
      }
    };
    onMounted(() => {

    });
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,      
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
</style>